package com.songtech.base.universal.dao.util;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.github.pagehelper.Page;
import com.songtech.base.universal.dao.api.beans.TableBeans;
import com.songtech.base.universal.dao.beans.BeanParam;
import com.songtech.base.universal.dao.constant.SQLExceptionConstant;

/**
 * 判断传入值是否抛异常
 * <br>有异常直接抛出
 */
public class ThrowsExceptionUtil {
	/**
	 * 抛出参数为null的异常
	 * @param objs
	 * @throws SQLException
	 */
	public static void throwNullParametersException(Object... objs) throws SQLException{
		for(Object obj : objs){
			if(obj == null){
				throw new SQLException(SQLExceptionConstant.SQL_EMPTY_PARAMETERS);
			}
			if(obj instanceof List){
				// 判断com.github.pagehelper.Page
				if(obj instanceof Page){
					if(((Page<?>) obj).getPageNum() <= 0 || ((Page<?>) obj).getPageSize() <= 0){
						throw new SQLException(SQLExceptionConstant.SQL_EMPTY_PARAMETERS_PAGE);
					}
				}else{
					if(((List<?>) obj).size() == 0 || ((List<?>) obj).get(0) == null){
						throw new SQLException(SQLExceptionConstant.SQL_EMPTY_PARAMETERS_LIST);
					}
				}
			}
			if(obj instanceof TableBeans){
				if(((TableBeans) obj).getEntityClass() == null || StringUtils.isEmpty(((TableBeans) obj).getTableName())){
					throw new SQLException(SQLExceptionConstant.SQL_EMPTY_PARAMETERS);
				}
			}
		}
	}
	
	/**
	 * 抛出空主键的异常
	 * @param bp
	 * @throws SQLException
	 */
	public static void throwNullPrimaryKeyException(BeanParam bp) throws SQLException{
		// 不存在主键
		if(bp == null || StringUtils.isEmpty(bp.getAttriName())){
			throw new SQLException(SQLExceptionConstant.SQL_NULL_PRIMARK_KEY);
		}
	}
	
	/**
	 * 抛出map为0 的异常
	 * @param map
	 * @throws SQLException
	 */
	public static void throwMapSizeEq0Exception(Map<?, ?> map) throws SQLException{
		if(map == null || map.size() == 0){
			// 抛出创建查询sql失败的异常信息
			throw new SQLException(SQLExceptionConstant.SQL_CREATE_QUERY_ERROR);
		}
	}
	
	/**
	 * 抛出传入sql为空的异常
	 * @param sql
	 * @throws SQLException
	 */
	public static void throwSqlIsNullException(String sql) throws SQLException{
		if(StringUtils.isEmpty(sql)){
			throw new SQLException(SQLExceptionConstant.SQL_IS_NULL);
		}
	}
}
